package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.LogUtil;

/**
 * Author: xurui 30031276
 * Created on 2022/9/29 15:03
 * Desc：
 */
public class LeaveDialog extends Dialog {
    private static final String TAG = LeaveDialog.class.getSimpleName();
    private LinearLayout sureLeave;
    private LinearLayout cancelLeave;
    private LinearLayout llContent;
    private CheckBox checkBox;

    public LeaveDialog(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.leave_conf_dialog);
        sureLeave = findViewById(R.id.sure_leave);
        cancelLeave = findViewById(R.id.cancel_leave);
        initListener();
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        super.show();
    }

    private void initListener() {
        if (ConfSdkPresenter.isChairman()) {
            //显示复选框
            llContent = findViewById(R.id.ll_content);
            llContent.setVisibility(View.VISIBLE);
            checkBox = findViewById(R.id.checkbox);
        }
        sureLeaveClick();
        cancelClick();
    }

    /**
     * 是否勾选了复选框
     *
     * @return 是或者否
     */
    public boolean isRadioChecked() {
        boolean isChecked = checkBox.isChecked();
        LogUtil.debug(TAG + " isChecked:" + isChecked);
        return isChecked;
    }

    /**
     * 点击取消
     */
    private void cancelClick() {
        cancelLeave.setOnClickListener((v -> {
            dismiss();
        }));
    }

    /**
     * 点击确定离开会议
     */
    private void sureLeaveClick() {
        sureLeave.setOnClickListener((v -> {
            int i;
            if (checkBox != null && isRadioChecked()) {
                i = ConfSdkPresenter.endConf();
            } else {
                i = ConfSdkPresenter.leaveConf();
            }
            LogUtil.info(TAG + " code: ===" + i);
            dismiss();
        }));
    }
}
